package it.unibz.inf.cn;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;

public class PropertyService {

	private Properties properties = new Properties();
	private File file = null;
	
	public PropertyService(String username) throws Exception{
		file = new File("res/"+username+".properties");
		if(!file.exists()){
			file.createNewFile();
			properties.put("accessToken", "");
			properties.put("accessTokenSecret", "");
			properties.store(new FileOutputStream(file), "");
		}else{
			properties.load(new FileInputStream(file));
		}
	}
	
	public String[] getToken(){
		String[] result = new String[2];
		String at = properties.getProperty("accessToken").trim();
		if(at == null || at.length()==0){
			return null;
		}else{
			result[0]= at;
		}
		String secret = properties.getProperty("accessTokenSecret").trim();
		if(secret == null || secret.length()==0){
			return null;
		}else{
			result[1]= secret;
		}
		return result;
	}
	
	public void storeToken(String token, String secret) throws Exception{
		properties.put("accessToken", token);
		properties.put("accessTokenSecret", secret);
		properties.store(new FileOutputStream(file), "");
	}
}
